﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using tmAsk.Common;

namespace tmAsk.Member
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        tmAsk.Data.MakehtmlSQL SQL = new tmAsk.Data.MakehtmlSQL();

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnOk_Click(object sender, EventArgs e)
        {
            function.CheckUserLogin();
            if (function.CheckNull(Request.Params["txtOldpwd"], Request.Params["txtPwd"], Request.Params["txtRepwd"]))
            {
                string oldpwd = Request.Params["txtOldpwd"].Replace("'", "");
                string txtPwd = Request.Params["txtPwd"].Replace("'", "");
                string txtRepwd = Request.Params["txtRepwd"].Replace("'", "");
                string uid = function.Getcookies();

                if (txtPwd != txtRepwd)
                {
                    Response.Write(function.alert_text(4, "新密码与确认密码不相同", ""));
                    return;
                }

                DataSet ds = SQL.GetList("b_user", "u_id='" + uid + "'", "u_password");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    function make_htm = new function();

                    if (ds.Tables[0].Rows[0]["u_password"].ToString() == make_htm.Md5(oldpwd))
                    {
                        string sql = "update b_user set u_password='" + make_htm.Md5(txtPwd) + "' where u_id='" + uid + "'";
                        if (SQL.ExecuteSql(sql) == 1)
                        {
                            Response.Write(function.alert_text(2, "修改成功", "ChangePassword.aspx"));
                        }
                        else
                        {
                            Response.Write(function.alert_text(4, "修改失败", ""));
                        }

                    }
                    else
                    {
                        Response.Write(function.alert_text(4, "旧密码错误", ""));
                        return;
                    }
                }
                else
                {
                    Response.Write(function.alert_text(2, "请重新登录", (new function()).Getlink("login", "")));
                    return;
                }
            }
            else
            {
                Response.Write(function.alert_text(4, "请将资料填写完整", ""));
                return;
            }
        }
    }
}